package com.xywy.beautyand;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.xywy.beautyand.fragment.BeauterfulBaFragment;
import com.xywy.beautyand.fragment.BeauterfunPlanFragment;
import com.xywy.beautyand.fragment.CountFragment;
import com.xywy.beautyand.fragment.MenuFragment;
import com.xywy.beautyand.fragment.PersionFragment;
/**
 * *****************************************  
* @Title: MainActivity.java 
* @Package com.xywy.beautyand 
* @Description: TODO(侧滑栏页面) 
* @author smz
* @date 2015-1-15 上午10:38:01 
* @version V1.0   
*******************************************
 */

public class MainActivity extends SlidingFragmentActivity implements OnClickListener {
 
	
	private Fragment mContent;
	private BeauterfulBaFragment experMsgFragment;//美吧
	private BeauterfunPlanFragment healthDataFragment;//妹妹计划
	private CountFragment settingFragment;//统计
	private PersionFragment persionFragment;//个人信息
	private FragmentManager fragmentManager;
	private TextView title;
//	private ImageView ivChangeView;//home按钮
//	private ImageView messageBtn;//消息按钮
	private boolean isRunning = false;
	private boolean isRegReceiver = false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.content_frame);
		initSlidingMenu();
		initFirstFragement(savedInstanceState);
		
		initView();
		setFragment("美美计划",R.id.tv_navi_beauty_plan);
	}

	@Override
	public void onClick(View v) {
		
	}
  
	private void initView(){
		
		RelativeLayout left_img=(RelativeLayout) findViewById(R.id.left_img);
		left_img.setVisibility(View.INVISIBLE);
		title = (TextView)findViewById(R.id.middle_title);
//		title.setText("美美计划");
	}
	/**
	 * 初始化侧边栏
	 */
	private void initSlidingMenu(){
		// 设置滑动菜单的视图
		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new MenuFragment()).commit();		

		// 实例化滑动菜单对象
		SlidingMenu sm = getSlidingMenu();
		// 设置滑动阴影的宽度
		sm.setShadowWidthRes(R.dimen.shadow_width);
		// 设置滑动阴影的图像资源
		sm.setShadowDrawable(R.drawable.shadow);
		// 设置滑动菜单视图的宽度
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// 设置渐入渐出效果的值
		sm.setFadeDegree(0.35f);
		// 设置触摸屏幕的模式
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);	
	}
	
	public void setFragment(String title,int id){
		toggle();
		FragmentTransaction transaction =  fragmentManager.beginTransaction();
		// 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
		hideFragments(transaction);
		this.title.setText(title);
		switch (id) {
		case R.id.tv_navi_beauty_plan:
			isRunning = false;
//			messageBtn.setVisibility(View.GONE);
			if (healthDataFragment == null) {
				// 如果MessageFragment为空，则创建一个并添加到界面上
				healthDataFragment = new BeauterfunPlanFragment();
				transaction.add(R.id.content_frame, healthDataFragment);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(healthDataFragment);
			}
			break;
		case R.id.tv_navi_beauty_bar:
			isRunning = true;
			if (experMsgFragment == null) {
				// 如果MessageFragment为空，则创建一个并添加到界面上
				experMsgFragment = new BeauterfulBaFragment();
				transaction.add(R.id.content_frame, experMsgFragment);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(experMsgFragment);
			}
			break;
		case R.id.tv_navi_statistics:
			isRunning = false;
			if (settingFragment == null) {
				// 如果MessageFragment为空，则创建一个并添加到界面上
				settingFragment = new CountFragment();
				transaction.add(R.id.content_frame, settingFragment);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(settingFragment);
			}
			break;
			
		case R.id.tv_navi_person_info:
			isRunning = false;
			if (persionFragment == null) {
				// 如果MessageFragment为空，则创建一个并添加到界面上
				persionFragment = new PersionFragment();
				transaction.add(R.id.content_frame, persionFragment);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(persionFragment);
			}
			break;
		}
		transaction.commit();
	}
	
	
	/**
	 * 将所有的Fragment都置为隐藏状态。
	 * 
	 * @param transaction
	 *            用于对Fragment执行操作的事务
	 */
	private void hideFragments(android.support.v4.app.FragmentTransaction ft) {
		if (experMsgFragment != null) {
			ft.hide(experMsgFragment);
		}
		if (healthDataFragment != null) {
			ft.hide(healthDataFragment);
		}
		if (settingFragment != null) {
			ft.hide(settingFragment);
		}
		if(persionFragment!=null)
			ft.hide(persionFragment);;
	}
	
	public void switchContent(Fragment fragment,String title) {
		mContent = fragment;
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, fragment)
		.commit();
		getSlidingMenu().showContent();
//		this.title.setText(title);
	}
	
	/**
	 * 初始化内容fragment
	 */
	private void initFirstFragement(Bundle savedInstanceState){
		//设置是否能够使用ActionBar来滑动
		setSlidingActionBarEnabled(true);
		setContentView(R.layout.content_frame);
		fragmentManager = getSupportFragmentManager();
	}
}
